package com.uls.web.controller.hj;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;
import com.uls.hj.domain.HjBookings;
import com.uls.hj.domain.HjVenues;
import com.uls.hj.service.IHjBookingsService;
import com.uls.hj.service.IHjVenuesService;
import com.uls.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.hj.domain.HjVenueSchedules;
import com.uls.hj.service.IHjVenueSchedulesService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 场馆使用排班Controller
 * 
 * @author hj
 * @date 2024-11-24
 */
@RestController
@RequestMapping("/hj/schedules")
public class HjVenueSchedulesController extends BaseController
{
    @Autowired
    private IHjVenueSchedulesService hjVenueSchedulesService;

    @Autowired
    private IHjVenuesService hjVenuesService;

    @Autowired
    private IHjBookingsService hjBookingsService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询场馆使用排班列表
     */
    @PreAuthorize("@ss.hasPermi('hj:schedules:list')")
    @GetMapping("/list")
    public TableDataInfo list(HjVenueSchedules hjVenueSchedules)
    {
        hjVenueSchedules.setDelFlag(0);
        startPage();
        List<HjVenueSchedules> list = hjVenueSchedulesService.selectHjVenueSchedulesList(hjVenueSchedules);

        for (HjVenueSchedules hjVenueSchedules1: list){
            HjVenues venues = hjVenuesService.selectHjVenuesByVenueId(hjVenueSchedules1.getVenueId());
            if (venues!=null){
                hjVenueSchedules1.setVenueName(venues.getVenueName());
            }

            HjBookings bookings = hjBookingsService.selectHjBookingsByBookingId(hjVenueSchedules1.getBookingId());
            if (bookings != null){
                SysUser sysUser = sysUserService.selectUserById(bookings.getUserId());
                if (sysUser != null){
                    hjVenueSchedules1.setBookingBy(sysUser.getNickName());
                    hjVenueSchedules1.setBookingAccount(sysUser.getUserName());
                    hjVenueSchedules1.setPhonenumber(sysUser.getPhonenumber());
                }
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出场馆使用排班列表
     */
    @PreAuthorize("@ss.hasPermi('hj:schedules:export')")
    @Log(title = "场馆使用排班", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HjVenueSchedules hjVenueSchedules)
    {
        List<HjVenueSchedules> list = hjVenueSchedulesService.selectHjVenueSchedulesList(hjVenueSchedules);
        ExcelUtil<HjVenueSchedules> util = new ExcelUtil<HjVenueSchedules>(HjVenueSchedules.class);
        util.exportExcel(response, list, "场馆使用排班数据");
    }

    /**
     * 获取场馆使用排班详细信息
     */
    @PreAuthorize("@ss.hasPermi('hj:schedules:query')")
    @GetMapping(value = "/{scheduleId}")
    public AjaxResult getInfo(@PathVariable("scheduleId") Long scheduleId)
    {
        return success(hjVenueSchedulesService.selectHjVenueSchedulesByScheduleId(scheduleId));
    }

    /**
     * 新增场馆使用排班
     */
    @PreAuthorize("@ss.hasPermi('hj:schedules:add')")
    @Log(title = "场馆使用排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HjVenueSchedules hjVenueSchedules)
    {
        return toAjax(hjVenueSchedulesService.insertHjVenueSchedules(hjVenueSchedules));
    }

    /**
     * 修改场馆使用排班
     */
    @PreAuthorize("@ss.hasPermi('hj:schedules:edit')")
    @Log(title = "场馆使用排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HjVenueSchedules hjVenueSchedules)
    {
        return toAjax(hjVenueSchedulesService.updateHjVenueSchedules(hjVenueSchedules));
    }

    /**
     * 删除场馆使用排班
     */
    @PreAuthorize("@ss.hasPermi('hj:schedules:remove')")
    @Log(title = "场馆使用排班", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scheduleIds}")
    public AjaxResult remove(@PathVariable Long[] scheduleIds)
    {
        //return toAjax(hjVenueSchedulesService.deleteHjVenueSchedulesByScheduleIds(scheduleIds));
        if (scheduleIds.length == 1) {
            Long scheduleId = scheduleIds[0];
            HjVenueSchedules venueSchedules = hjVenueSchedulesService.selectHjVenueSchedulesByScheduleId(scheduleId);
            venueSchedules.setDelFlag(1);
            return toAjax(hjVenueSchedulesService.updateHjVenueSchedules(venueSchedules));
        }
        Integer delFlag = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("delFlag", delFlag);
        params.put("scheduleIds", scheduleIds);
        return toAjax(hjVenueSchedulesService.updateHjVenueSchedulesByScheduleIds(params));
    }
}
